﻿using Prism.Commands;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model;
using SmartPlant.Model.Biz;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using SmartPlant.WinCore.Helper;
using System.Windows.Input;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class TemplateInputSettingNewViewModel : DialogViewModelBase, IDialogAware
    {
        private int _templateType = 0;
        private DelegateCommand<object> _okDialogCommand;
        private IAchievementService _specOutputService;
        private IImportUtilityService _importUtilityService;

        public TemplateInputSettingNewViewModel(
            IAchievementService specOutputService,
            IImportUtilityService importUtilityService,
            IDialogService dialogService) : base(dialogService)
        {
            Title = "新建模板";
            _specOutputService = specOutputService;
            _importUtilityService = importUtilityService;
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            base.OnDialogOpened(parameters);
            _templateType = parameters.GetValue<int>("templateType");
            if (_templateType == 0)
                ComboxList.Add(new ComboBoxItemModel<SimpleIntModel> { Description = "规格书", IsEnable = true, SelectedModel = new SimpleIntModel { v1 = 0 } });
            if (_templateType == 1)
                ComboxList.Add(new ComboBoxItemModel<SimpleIntModel> { Description = "索引表", IsEnable = true, SelectedModel = new SimpleIntModel { v1 = 1 } });
            if (_templateType == 2)
                ComboxList.Add(new ComboBoxItemModel<SimpleIntModel> { Description = "安装图", IsEnable = true, SelectedModel = new SimpleIntModel { v1 = 2 } });
            if (ComboxList.Count > 0)
                this.SelectedModel = ComboxList[0].SelectedModel;
        }

        public List<ComboBoxItemModel<SimpleIntModel>> ComboxList { get; set; } = new List<ComboBoxItemModel<SimpleIntModel>>();

        private SimpleIntModel _selectedModel;
        public SimpleIntModel SelectedModel
        {
            get { return _selectedModel; }
            set { SetProperty(ref _selectedModel, value); }
        }

        private string _templateName = "";
        public string TemplateName
        {
            get { return _templateName; }
            set { SetProperty(ref _templateName, value); }
        }

        private string _generateFunc = "";
        public string GenerateFunc
        {
            get { return _generateFunc; }
            set { SetProperty(ref _generateFunc, value); }
        }

        private string _templatePath = "";
        public string TemplatePath
        {
            get { return _templatePath; }
            set { SetProperty(ref _templatePath, value); }
        }

        private string _description = "";
        public string Description
        {
            get { return _description; }
            set { SetProperty(ref _description, value); }
        }

        private string _sql = "";
        public string SQL
        {
            get { return _sql; }
            set { SetProperty(ref _sql, value); }
        }

        private int _InOrOut = 0;

        public int InOrOut
        {
            get { return _InOrOut; }
            set { SetProperty(ref _InOrOut, value); }
        }


        public ICommand OkDialogCommand
        {
            get
            {
                if (_okDialogCommand != null) return _okDialogCommand;
                _okDialogCommand = new DelegateCommand<object>(onOkDialogCommand);
                return _okDialogCommand;
            }
        }


        private async void onOkDialogCommand(object parameter)
        {
            string proId = GlobalObject.CurrentProject.ProjId.ToString();

            if (_selectedModel == null)
            {
                Dialog.ShowAltert("您的选择不规范，请选择新建模板类型。");
                return;
            }
            if (_selectedModel.v1.IsNullOrEmpty())
            {
                Dialog.ShowAltert("您的选择不规范，请选择新建模板类型。");
                return;
            }

            if (_templateName.IsNullOrEmpty())
            {
                Dialog.ShowAltert("您的输入不规范，模板名称不能为空。");
                return;
            }
            if (_templatePath.IsNullOrEmpty())
            {
                Dialog.ShowAltert("您的输入不规范，模板路径不能为空。");
                return;
            }
            try
            {
                Report report = new Report
                {
                    Description = _description,
                    GenerateFunc = GenerateFunc,
                    Name = _templateName,
                    SQL = _sql,
                    TemplatePath = _templatePath,
                    Type = (TemplateType)_templateType,
                    InOrOut = InOrOut
                };

                ITemplateSettingService templateSettingService = _templateType == 1 ? _importUtilityService : _specOutputService;
                var r = await templateSettingService.UploadTemplateFileAsync(proId, report);
                if (r != null)
                {
                    Dialog.ShowAltert("上传文件至远程服务器成功！！！");
                    RaiseRequestClose(new DialogResult(ButtonResult.OK));
                }
                else
                {
                    RaiseRequestClose(new DialogResult(ButtonResult.None));
                }
            }
            catch (Exception x)
            {
                var xMessage = $"上传文件至远程服务器失败：{x.Message}！！！";
                LoggerHelper.Current.Error(xMessage, x);
                Dialog.ShowAltert(xMessage);
            }
        }
    }
}
